using System;
using System.Collections.Generic;
using System.Linq;

namespace todictionary
{
    public static class StringExtensions
    {
        public static IDictionary<string, string> ToDictionary(this string config) {
            return new Config(config)
                .SplitIntoSettings()
                .SplitIntoParameterAndValues()
                .CreateDictionary();
        }
            
        internal static IDictionary<string, string> CreateDictionary(this IEnumerable<Tuple<Parameter, Value>> tuples) {
            return tuples.ToDictionary(tuple => tuple.Item1.String, tuple => tuple.Item2.String);
        }

        internal static IEnumerable<Tuple<Parameter, Value>> SplitIntoParameterAndValues(this IEnumerable<Setting> settings) {
            foreach (var setting in settings) {
                var parts = setting.String.Split('=');
                yield return new Tuple<Parameter, Value>(new Parameter(parts[0]), new Value(parts[1]));
            }
        }

        internal static IEnumerable<Setting> SplitIntoSettings(this Config config) {
            var settings = config.String.Split(';');
            return settings.Select(setting => new Setting(setting));
        }
    }
}